package lt.liepasoft.locr.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListSelectionListener;

import lt.liepasoft.locr.ImageManager;
import lt.liepasoft.locr.data.Symbol;
import lt.liepasoft.locr.data.SymbolImage;
import lt.liepasoft.locr.utils.ImageUtils;

public class OcrSymbolsList extends JPanel {
    
    
    /**
	 * 
	 */
	private static final long serialVersionUID = -3431869279433910888L;
	
	private DefaultListModel <OcrSymbolsList.SymbolCell> model;
    private JList<OcrSymbolsList.SymbolCell> list;
    private JLabel symbolCount;
    private ImageUtils imageUtils;
    
    public OcrSymbolsList() {
        super(new BorderLayout());
        imageUtils = new ImageUtils();
        model = new DefaultListModel<>();
        list = new JList<>(model);
        list.setCellRenderer(new SymbolCellRenderer());
        JScrollPane scrollPane = new JScrollPane(list);        
        scrollPane.setPreferredSize(new Dimension(150, 400));     
        //scrollPane.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        
        JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER));
       // p.setBorder(BorderFactory.createEmptyBorder(15, 15, 0, 15));
        symbolCount = new JLabel(){
            @Override
            public String getText() {
                setText("Symbols: "+model.size());
                return super.getText();
            }            
        };
        p.setPreferredSize(new Dimension(p.getWidth(), 32));
        p.add(symbolCount);
        
        this.add(p, BorderLayout.NORTH);
        this.add(scrollPane, BorderLayout.CENTER);
    }
    
    
    public void setSymbolsList(List <Symbol> symbolsList){     
        model.clear();
        for (Symbol symbol: symbolsList){
            model.addElement(new SymbolCell(
            		symbol, createImageIcon(symbol.getImage())
                )
            );
        }        
        updateUI();
    }
   
    public ImageIcon createImageIcon(SymbolImage img){
        if (img != null){
            BufferedImage bimg = imageUtils.rgbImageToImage(ImageManager.createRgbImage(img));
            bimg = ImageUtils.getInstance().scaleImage(bimg, 50, 50, false);            
            return new ImageIcon(bimg);
        }else{
            return null;
        }
    }
    
    public void addListSelectionListener(ListSelectionListener l){
        list.addListSelectionListener(l);
    }
    
    public Symbol getSelectedSymbol(){
        Object o = list.getSelectedValue();

        if (o != null){
            return ((SymbolCell)o).symbol;
        }
        return null;                    
    }
    
    public void clear(){
        model.clear();
    }
    
    class SymbolCell {
        public final Symbol symbol;
        public final ImageIcon symbolImg;
//        public final SymbolDrawPanel symbolPanel;
        
        public SymbolCell(Symbol symbol, ImageIcon symbolImg) {
            this.symbol = symbol;
            this.symbolImg = symbolImg;
//            symbolPanel = new SymbolDrawPanel(50, 50, symbol.getImage());
        }
    }
    
    class SymbolCellRenderer extends JLabel implements ListCellRenderer <OcrSymbolsList.SymbolCell> {

        private Color HIGHLIGHT_COLOR = new Color(0, 0, 128);
        
        public SymbolCellRenderer() {
            setOpaque(true);
//            setIconTextGap(12);
        }
        

		@Override
		public Component getListCellRendererComponent(
				JList<? extends SymbolCell> list, SymbolCell cell, int index,
				boolean isSelected, boolean cellHasFocus) {
            this.setIcon(cell.symbolImg);
//            this.removeAll();
//            this.add(cell.symbolPanel);
            if (isSelected) {
                setBackground(HIGHLIGHT_COLOR);
                setForeground(Color.white);
            } else {
                setBackground(Color.white);
                setForeground(Color.black);
            }
            this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            return this;
		}
        
    }
}
